/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.components.custom;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import org.biz.util.ReflectionUtility;
import org.components.parent.controls.PxTable;

/**
 *
 * @author yy
 */
public class GridControllerPanel extends javax.swing.JPanel {

    
    private GridController controller;
    private PxTable table;
    public void setTable(PxTable ct){
        table=ct;
    }
    
    /**
     * Creates new form GridController
     */
    public GridControllerPanel() {
        initComponents();
        //add - addaction
       tadd.setAction(new AbstractAction("Add") {

            @Override
            public void actionPerformed(ActionEvent e) {
                addAction();
            }
        });
       tadd.setText("Add");
       tremove.setAction(new AbstractAction("Remove") {

            @Override
            public void actionPerformed(ActionEvent e) {
                removeAction();
            }
        });
       tremove.setText("Remove");
        tcopy.setAction(new AbstractAction("Copy") {

            @Override
            public void actionPerformed(ActionEvent e) {
                copyAction();
            }
        });
        tcopy.setText("Copy");
        
         tclear.setAction(new AbstractAction("Clear") {

            @Override
            public void actionPerformed(ActionEvent e) {
                clearAction();
            }
        });
         tclear.setText("Clear");
         
         
         tup.setAction(new AbstractAction("UP") {

            @Override
            public void actionPerformed(ActionEvent e) {
            table.moveSelectedLineUp();    
            }
        });
         

        tdown.setAction(new AbstractAction("Down") {
            @Override
            public void actionPerformed(ActionEvent e) {
            table.moveSelectedLineDown();
            }
        });
         
       //set "add" - "myaddaction"
    }
    
    public void setGridController(GridController controller1) {
        this.controller = controller1;
    }

    public void addAction() {
        Class cls = table.getModelClass();
        try {
            Object ob = ReflectionUtility.getDynamicInstance(cls);
            table.addModelToTable(ob);
        } catch (Exception e) {
        }
    }

    public void removeAction() {
        table.removeSelectedRow();
    }

    public void copyAction() {
        table.copySelectedElement();
    }

    public void clearAction() {
        table.clear();

    }
    
    public void addAction(ActionListener action,JButton btn){        
        btn.addActionListener(action);
    }
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tadd = new org.components.controls.CButton();
        tcopy = new org.components.controls.CButton();
        tremove = new org.components.controls.CButton();
        tup = new org.components.controls.CButton();
        tdown = new org.components.controls.CButton();
        tclear = new org.components.controls.CButton();
        tclearline = new org.components.controls.CButton();

        setLayout(null);

        tadd.setText("Add");
        tadd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                taddActionPerformed(evt);
            }
        });
        add(tadd);
        tadd.setBounds(10, 11, 70, 30);

        tcopy.setText("Copy");
        tcopy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tcopyActionPerformed(evt);
            }
        });
        add(tcopy);
        tcopy.setBounds(10, 70, 70, 30);

        tremove.setText("Remove");
        tremove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tremoveActionPerformed(evt);
            }
        });
        add(tremove);
        tremove.setBounds(10, 40, 70, 30);

        tup.setText("Up");
        tup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tupActionPerformed(evt);
            }
        });
        add(tup);
        tup.setBounds(10, 100, 70, 30);

        tdown.setText("Down");
        tdown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tdownActionPerformed(evt);
            }
        });
        add(tdown);
        tdown.setBounds(10, 130, 70, 30);

        tclear.setText("Clear");
        tclear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tclearActionPerformed(evt);
            }
        });
        add(tclear);
        tclear.setBounds(10, 160, 70, 30);

        tclearline.setText("Clear Line");
        tclearline.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tclearlineActionPerformed(evt);
            }
        });
        add(tclearline);
        tclearline.setBounds(10, 190, 70, 30);
    }// </editor-fold>//GEN-END:initComponents

    private void taddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_taddActionPerformed
    }//GEN-LAST:event_taddActionPerformed

    private void tcopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tcopyActionPerformed
//        tblInvoiceLine1.copySelectedElement();
    }//GEN-LAST:event_tcopyActionPerformed

    private void tupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tupActionPerformed
//        tblInvoiceLine1.moveSelectedLineUp();

    }//GEN-LAST:event_tupActionPerformed

    private void tclearlineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tclearlineActionPerformed
        //get selected line > get rows cell values > clear

        //get detail panel of the table if exists > call clear

    }//GEN-LAST:event_tclearlineActionPerformed

    private void tclearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tclearActionPerformed
//        tblInvoiceLine1.clear();
    }//GEN-LAST:event_tclearActionPerformed

    private void tdownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tdownActionPerformed
//        tblInvoiceLine1.moveSelectedLineDown();
    }//GEN-LAST:event_tdownActionPerformed

    private void tremoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tremoveActionPerformed
//        tblInvoiceLine1.removeSelectedRow();
    }//GEN-LAST:event_tremoveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.components.controls.CButton tadd;
    private org.components.controls.CButton tclear;
    private org.components.controls.CButton tclearline;
    private org.components.controls.CButton tcopy;
    private org.components.controls.CButton tdown;
    private org.components.controls.CButton tremove;
    private org.components.controls.CButton tup;
    // End of variables declaration//GEN-END:variables
}
